import json
from flask import Flask,jsonify,request
from config.config import mock_data_file

# 实例化一个flask应用
app = Flask(__name__)

# 读取mock的数据配置文件
with open(mock_data_file,"r") as file:
    mock_data = json.load(file)

# /<path:path>表示接受任何路由地址
@app.route("/<path:path>",methods=["GET","POST","PUT","DELETE"])
def basedata_query(path):

    # 拼接配置文件的键
    method = request.method
    key = method.upper()+f" {path}"

    # 遍历配置文件里的key 和 value
    for k,v in mock_data.items():
        if key == k:
            code = v.get("code",200)
            response = v.get("data",{})

            # 返回数据以及http状态码
            return jsonify(response),code


if __name__ == '__main__':
    app.run(debug=True)